﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ProjetPacman;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pacman
{
    class Fantome : Personnage
    {
        public const int GOLD = 1;
        public const int RED = 2;
        public const int BLUE = 3;
        public const int PINK = 4;

        private bool eated;
        private int[,] howtokillpacman;
        private Vector2 destination;

        public String reset(int niveau, int type)
        {
            String temp, root="";
            if (niveau == 1)
            {
                Position=new Vector2(13f, 15f);
                switch (type)
                {
                    case GOLD:
                        PositionX -=2f;
                        temp = "gold";
                        break;
                    case RED:
                        PositionX += 3f;
                        temp = "red";
                        break;
                    case BLUE:
                        PositionX -= 2f;
                        PositionY -= 1f;
                        temp = "blue";
                        break;
                    case PINK:
                        PositionX += 3f;
                        PositionY -= 1f;
                        temp = "pink";
                        break;
                    default:
                        temp = "gold";
                        break;
                }
                root = "Images/:colorGhost";
                root = root.Replace(":color", temp);
                Direction = BAS;
                _vulnerable = false;
                eated = false;
            }
            return root;
        }

        public Fantome(ContentManager content, int type,Levels level, int niveau)
            :base (niveau)
        {
            _vitesse = NORMAL;
            Texture2D image = content.Load<Texture2D>(reset(niveau,type));

            for (int i = 0; i < 4; i++)
            {
                Animation a = new Animation();

                a.AddFrame(new Frame(image, new Rectangle(i * 40, 0, 20, 20), 250));
                a.AddFrame(new Frame(image, new Rectangle(i * 40+20, 0, 20, 20), 250));
                animations.Add(a);
            }

            image = content.Load<Texture2D>("Images/sickGhost");
            Animation b = new Animation();
            b.AddFrame(new Frame(image, new Rectangle(0, 0, 20, 20), 250));
            b.AddFrame(new Frame(image, new Rectangle(20, 0, 20, 20), 250));
            animations.Add(b);

            image = content.Load<Texture2D>("Images/eatenGhost");
            b = new Animation();
            for (int i = 0; i < 4; i++)
            {
                b.AddFrame(new Frame(image, new Rectangle(i * 20, 0, 20, 20), 250));
                animations.Add(b);
            }

            resetMap(level);
            destination = new Vector2(1f, 1f);
        }

        public void resetMap(Levels level)
        {
            howtokillpacman = level.getMapFantome(eated);
        }

        public Vector2 SurleBord(Vector2 position)
        {
            if (position.X <= 1f) position.X=1f; 
            if (position.Y == 1f) position.Y=1f;
            if (position.X >= Levels.VY - 2f) 
                position.X=Levels.VY-2f;
            if (position.Y >= Levels.VX - 2f) 
                position.Y=Levels.VX-2f;
            return position;
        }

        public void orientation(int orientation)
        {
            int next;
            switch (orientation)
            {
                case 1:
                    NextDirection = GAUCHE;
                    break;
                case 2:
                    NextDirection = HAUT;
                    break;
                case 3:
                    NextDirection = BAS;
                    break;
                case 4:
                    NextDirection = DROITE;
                    break;
            }
            next = NextDirection;
            if (_vulnerable) 
                next = 4;
            if (eated)
                next = 5 + NextDirection;
            if (next != Direction) animations.Start(Direction);
        }

        public int Update(GameTime gameTime, Levels level, Vector2 position)
        {
            Deplacements deplacement = new Deplacements();
            level.posFantome(Position);
            position=SurleBord(position);

            if (position != destination && !_vulnerable && !eated) //Pacman bouge
            {
                destination = position;
                resetMap(level);
                deplacement.chemin(Position, destination, howtokillpacman);
            }

            if (!_vulnerable && !eated)// j'attaque normalement
            {
                if (finAnimation()) orientation((int)deplacement.seRapprocher(howtokillpacman, Position, 1).Z);
            }
            
            else if (_vulnerable)// pacman a mangé une pacgum
            {
                if (position == Position)// je me suis fait manger
                {
                    eated = true;
                    _vulnerable = false;
                    _vitesse = RAPIDE;

                    while (!finAnimation()) Next(NextDirection);//On termine le mouvement

                    destination = new Vector2(13f, 15f);
                    resetMap(level);
                    deplacement.chemin(Position, destination, howtokillpacman);
                    orientation((int)deplacement.seRapprocher(howtokillpacman, Position, 1).Z);
                }
                else if (finAnimation()) orientation((int)deplacement.sEloigner(howtokillpacman, Position, 1).Z);
            }
            else if (eated) // pacman m'a mangé : je rentre chez moi
            {
                if (Position == destination)// je suis rentré je réattaque !
                {
                    eated = false;
                    _vitesse = NORMAL;

                    while (!finAnimation()) Next(NextDirection);//On termine le mouvement

                    destination = position;
                    resetMap(level);
                    deplacement.chemin(Position, destination, howtokillpacman);
                    orientation((int)deplacement.seRapprocher(howtokillpacman, Position, 1).Z);
                }
                else
                {
                    if (finAnimation()) orientation((int)deplacement.seRapprocher(howtokillpacman, Position, 1).Z);
                }
            }
            Next(NextDirection);

            level.posFantome(Position);

            if (position != Position) return 0;
            else return 1;
        }
    }
}
